package priv.bestbeat.reportcenter.core.mq.rocket;

import lombok.NoArgsConstructor;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import priv.bestbeat.reportcenter.core.monitor.Observable;
import priv.bestbeat.reportcenter.core.mq.Message;
import priv.bestbeat.reportcenter.core.mq.MessageProxy;

import javax.annotation.Resource;

/**
 * @author 张渠钦
 * @date 2022/4/24 17:16
 */
@NoArgsConstructor
public class SpringRocketMQProxy extends MessageProxy<RocketMessage> {

    @Resource
    private RocketMQTemplate rocketMQTemplate;

    public SpringRocketMQProxy(RocketMQTemplate rocketMQTemplate) {
        this.rocketMQTemplate = rocketMQTemplate;
    }

    @Override
    public RocketMessage send(RocketMessage message) throws Exception {
        rocketMQTemplate.send(message.getTopic() + ":" + message.getTags(), message);
        message.sendDone();

        return message;
    }


    @Override
    protected RocketMessage wrap(RocketMessage message, Observable observable) {
        RocketMessage<Observable> rocketMessage = new RocketMessage<>();
        rocketMessage.setTags(message.getTags());
        rocketMessage.setTopic(message.getTopic());
        rocketMessage.setPayload(observable);
        rocketMessage.setHeaders(message.getHeaders());
        return rocketMessage;
    }
}
